/*
 * DirectedDiffusion.java
 *
 * @created Mercredi 04/04/07
 *
 * @author bbrison
 */

package communication.thirdLayer;

import java.util.*;
import kernel.*;
import objects.*;
import communication.*;
import communication.pack.*;


public class DirectedDiffusion extends SensorRouting {
    
    private HashMap<Integer,DirectedResults> _results;

    private HashMap<Integer,TimeOut> _timeout;
    
    
    public DirectedDiffusion() {
	super((SensorSimulator)Simulator.getSimulator(), SensorRouting.DIRECTED);
	_results = new HashMap<Integer,DirectedResults>();
	_timeout = new HashMap<Integer,TimeOut>();
    }
    
    public DirectedDiffusion(SensorSimulator sim) {
	super(sim, SensorRouting.DIRECTED);
	_results = new HashMap<Integer,DirectedResults>();
	_timeout = new HashMap<Integer,TimeOut>();
    }
    
    public void packagesTreatment(){
	Vector<Router> ntwork = getNetwork();
	for(Router current : ntwork) {
	    LinkedList<IdentifiedCurrentPackage> packages = (this.getMySim().getCommunicationManager()
							     .getTLManager().getCurrentPackages()).myPackages(current.getStation());
	    
	    for (int j = 0; j < packages.size(); j++) {
		IdentifiedCurrentPackage cr = packages.removeFirst();
		if (!cr.isReceiver()) {
		    current.packageTreatment(cr.getCurrentPackage(), cr.getCurrentStation());
		}
	    }
	}
    }
    
    public HashMap<Integer,DirectedResults> getResults() {
	return _results;
    }
    
    public HashMap<Integer,TimeOut> getTimeOut() {
	return _timeout;
    }
    
    public void addResult(Integer key, DirectedResults res) {
	_results.put(key,res);
    }
    
    public void endTimeOut(Integer x) {
	_timeout.get(x).setOver();
	_timeout.remove(x);
    }

    public void reLaunchTimeOut(Integer x) {
	_timeout.get(x).setStart(Simulator.getSimulator().getTime());
    }
    
    public void launchTimeOut(Integer x, double start) {
	TimeOut to = new TimeOut(Simulator.getSimulator(),x, start);
	_timeout.put(x,to);
	to.start();
    }

    public void routesMaintenance(){}
    public void failedCommunicationTreatment(WishedEmission we){
	System.out.println("failedCommunication");
    }
    
    
}
